<?php

namespace app\admin\controller;

use think\facade\Db;
use app\admin\model\AuthGroup;
use think\facade\Request;
use think\Validate;
use think\facade\View;
use app\admin\model\AuthAdmin as authAdminModel;

class AuthAdmin extends Common
{


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 管理员列表
     * @author admin
     * @date 2019-08-08
     */
    public function index()
    {

        if (request()->isPost()) {
            $val = input('val');
            $url['val'] = $val;
            $this->assign('testval', $val);
            $map = [];
            if ($val) {
                $map[] = ['username|email|tel', 'like', "%" . $val . "%"];
            }
            if (session(config('auth.USER_AUTH_KEY')) != 1) {
                $map[] = ['a.id', '=', session(config('auth.USER_AUTH_KEY'))];
            }
            $list = Db::name('auth_admin')->alias('a')
                ->join('auth_group ag', 'a.group_id = ag.group_id', 'left')
                ->field('a.*,ag.title')
                ->where($map)
                ->select()->toArray();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list, 'rel' => 1];
        }
        return View::fetch();
    }


    /**
     * 编缉管理员
     * @author admin
     * @date 2019-08-08
     */
    public function edit()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $pwd = input('post.u_pwd');
                $where['id'] = $data['id'];
                if ($pwd) {
                    $data['u_pwd'] = md5(input('post.u_pwd'));
                } else {
                    unset($data['u_pwd']);
                }
                authAdminModel::update($data, $where);
                return ['code' => 0, 'msg' => '操作成功!'];
            } else {
                $id = input("get.id", 0, "intval");
                if (!$id) return ['code' => 3000, 'msg' => '非法请求'];
                $auth_group = Db::name("auth_group")->select();
                $info = Db::name("auth_admin")->where("id", $id)->find();
                unset($info['u_pwd']);
                $this->assign('info', json_encode($info, true));
                $this->assign('authGroup', $auth_group);
                return view();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 添加管理员
     * @author admin
     * @date 2019-08-08
     */
    public function add()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $pwd = input('post.u_pwd', 0, "trim");
                if (!$pwd) {
                    return ['code' => 1, 'msg' => '密码不能为空！'];
                }
                $data['u_pwd'] = md5($pwd);

                $authAdminModel = new authAdminModel();
                $result = $authAdminModel->save($data);
                //添加
                if ($result) {
                    return ['code' => 0, 'msg' => '操作成功!', 'url' => url('adminList')];
                } else {
                    return ['code' => 1, 'msg' => '操作失败!'];
                }
            } else {
                $auth_group = Db::name("auth_group")->select();
                $this->assign('authGroup', $auth_group);
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }
	

    /**
     * 改变管理员状态
     * @author admin
     * @date 2019-08-08
     */
    public function doStatus()
    {
        try {
            $id = input('post.id');
            $status = input('post.status');
            if ($id == 1) {
                return ['code' => 1, 'msg' => '超级管理员状态不可改变!'];
            }
            $result = Db::name("auth_admin")->where("id", $id)->save(['status' => $status]);
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            } else {
                return ['code' => 2000, 'msg' => '操作失败 !'];
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 删除管理员
     * @author admin
     * @date 2019-08-08
     */
    public function doDelete()
    {
        try {
            $admin_id = input('post.id');
            if ($admin_id == 1) {
                return ['code' => 1, 'msg' => '超级管理员不可删除!'];
            }
			if($admin_id==session(config("auth.USER_AUTH_KEY"))){
				return ['code' => 1, 'msg' => '不能删除自己!'];	
			}
            if (authAdminModel::destroy($admin_id)) {
                return ['code' => 0, 'msg' => '操作成功!'];
            } else {
                return ['code' => 1, 'msg' => '操作失败!'];
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }
}